package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.dto.Dto;
import com.bean.pojo.*;
import com.bean.vo.hotel.HotelVideoDescVO;
import com.bean.vo.hotel.ItripSearchDetailsHotelVO;
import com.bean.vo.hotel.ItripSearchPolicyHotelVO;
import com.service.*;
import com.util.DtoUtil;
import com.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;


@RestController
public class HotelController {

    @Autowired
    private IItripAreaDicService iItripAreaDicService;
    @Autowired
    private IItripLabelDicService iItripLabelDicService;
    @Autowired
    private IItripImageService iItripImageService;
    @Autowired
    private IItripHotelService iItripHotelService;
    @Autowired
    private IItripHotelFeatureService itripHotelFeatureService;
    @Autowired
    private IItripLabelDicService itripLabelDicService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/api/hotel/querytradearea/{cityId}")
    public Dto querytradearea(@PathVariable("cityId") Integer cityId) {
        System.out.println(">>> 根据城市查询商圈");

        System.out.println("城市id" + cityId);

        if (cityId == null) {
            return DtoUtil.returnError("10203 ", "cityId不能为空", "");
        }

        QueryWrapper<ItripAreaDic> wrapper = new QueryWrapper<>();
        wrapper.eq("isTradingArea", 1);
        wrapper.eq("parent", cityId);

        List<ItripAreaDic> list = iItripAreaDicService.list(wrapper);

        if (list.size() > 0) {
            return DtoUtil.returnSuccess("查询成功", list);
        } else {
            return DtoUtil.returnError("100213 ", "系统异常,获取失败", "");
        }
    }

    @GetMapping("/api/hotel/getimg/{targetId}")
    public Dto getimg(@PathVariable("targetId") String targetId) {
        System.out.println(">>> 查询酒店图片");

        System.out.println("酒店id" + targetId);

        if (targetId == null) {
            return DtoUtil.returnError("100213 ", "酒店id不能为空", "");
        }

        QueryWrapper<ItripImage> wrapper = new QueryWrapper<>();
        wrapper.eq("targetId", targetId);
        wrapper.eq("type", 0);

        List<ItripImage> list = iItripImageService.list(wrapper);

        if (list.size() > 0) {
            return DtoUtil.returnSuccess("查询成功", list);
        } else {
            return DtoUtil.returnError("100212 ", "获取酒店图片失败", "");
        }
    }

    @GetMapping("/api/hotel/getvideodesc/{hotelId}")
    public Dto getvideodesc(@PathVariable("hotelId") String hotelId) {
        System.out.println(">>> 根据酒店id查询酒店特色、商圈、酒店名称");
        System.out.println("酒店:" + hotelId);

        if (hotelId == null) {
            System.out.println("1");
            return DtoUtil.returnError("100215 ", "酒店id不能为空", "");
        }

        //需要返回的对象vo
        HotelVideoDescVO hvdv = new HotelVideoDescVO();

        //根据酒店id获取酒店名称
        ItripHotel hotelById = iItripHotelService.getById(hotelId);
        System.out.println("酒店名字"+hotelById.getHotelName());
        hvdv.setHotelName(hotelById.getHotelName());

        //根据酒店id获取商圈
        QueryWrapper<ItripAreaDic> wrapper = new QueryWrapper<>();
        wrapper.eq("parent",hotelById.getCityId() );
        wrapper.eq("isTradingArea", 1);

        List<ItripAreaDic> list = iItripAreaDicService.list(wrapper);
        List<String> stringList = new ArrayList<>();
        System.out.println("找到的商圈名");
        for (ItripAreaDic i:
        list) {
            stringList.add(i.getName());
            System.out.println(i.getName());
        }
        hvdv.setTradingAreaNameList(stringList);

        //根据酒店id获取特色
            //先获取特色服务id
        QueryWrapper<ItripHotelFeature> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("hotelId",hotelId);
        List<ItripHotelFeature> list1 = itripHotelFeatureService.list(wrapper2);
        System.out.println("特色服务id");
        List<String> stringList1 = new ArrayList<>();

        for (ItripHotelFeature i:
        list1) {
            System.out.println(i.getFeatureId());
            QueryWrapper<ItripLabelDic> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("id",i.getFeatureId());
            stringList1.add(itripLabelDicService.getOne(wrapper3).getName());
        }

        for (String a:
        stringList1) {
            System.out.println(a);
        }

        hvdv.setHotelFeatureList(stringList1);

        System.out.println(hvdv);

        return DtoUtil.returnSuccess("查询成功", hvdv);
    }

    @GetMapping("/api/hotel/queryhotelfacilities/{id}")
    public Dto queryhotelfacilities(@PathVariable("id") Integer id) {
        System.out.println(">>> 根据酒店id查询酒店设施");
        System.out.println("酒店id:" + id);

        if(id==null){
            return DtoUtil.returnError("10206 ", "酒店id不能为空", "");
        }

        QueryWrapper<ItripHotel> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);

        ItripHotel itripHotel = iItripHotelService.getOne(wrapper);

        if (itripHotel !=null) {
            return DtoUtil.returnSuccess("查询成功", itripHotel.getFacilities());
        } else {
            return DtoUtil.returnError("10207 ", "系统异常", "");
        }
    }

    @GetMapping("/api/hotel/queryhotcity/{type}")
    public Dto queryhotcity(@PathVariable("type") Integer type) {
        System.out.println(">>> 查询热门城市");
        System.out.println("城市:" + type);

        QueryWrapper<ItripAreaDic> wrapper = new QueryWrapper<>();
        wrapper.eq("isHot", 1);
        wrapper.eq("level", 2);
        wrapper.eq("isChina", type);

        List<ItripAreaDic> list = iItripAreaDicService.list(wrapper);

        if (list.size() > 0) {
            return DtoUtil.returnSuccess("查询成功", list);
        } else if (list.size() == 0) {
            return DtoUtil.returnError("10201", "查询失败", "");
        } else {
            return DtoUtil.returnError("10202 ", "系统异常", "");
        }
    }

    @GetMapping("/api/hotel/queryhotelfeature")
    public Dto queryhotelfeature() {
        System.out.println(">>> 查询酒店特色列表");

        QueryWrapper<ItripLabelDic> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId", 16);

        List<ItripLabelDic> list = iItripLabelDicService.list(wrapper);

        if (list.size() > 0) {
            return DtoUtil.returnSuccess("查询成功", list);
        } else {
            return DtoUtil.returnError("10205", "查询失败", "");
        }
    }

    @GetMapping("/api/hotel/queryhoteldetails/{id}")
    public Dto queryhoteldetails(@PathVariable("id") Integer id) {
        System.out.println(">>> 根据酒店id查询酒店特色和介绍ItripSearchDetailsHotelVO");
        System.out.println("酒店id:" + id);

        if (id == null) {
            System.out.println("1");
            return DtoUtil.returnError("100215 ", "酒店id不能为空", "");
        }

        //根据酒店id获取特色
        //先获取特色服务id
        QueryWrapper<ItripHotelFeature> wrapper = new QueryWrapper<>();
        wrapper.eq("hotelId",id);
        List<ItripHotelFeature> featureIdList = itripHotelFeatureService.list(wrapper);
        System.out.println("特色服务id");
        List<ItripSearchDetailsHotelVO> stringList1 = new ArrayList<>();

        for (ItripHotelFeature i:
                featureIdList) {
            //需要返回的对象vo
            ItripSearchDetailsHotelVO isdhv = new ItripSearchDetailsHotelVO();

            System.out.println(i.getFeatureId());
            QueryWrapper<ItripLabelDic> wrapper3 = new QueryWrapper<>();
            wrapper3.eq("id",i.getFeatureId());
            isdhv.setName(itripLabelDicService.getOne(wrapper3).getName());
            isdhv.setDescription(itripLabelDicService.getOne(wrapper3).getDescription());

            stringList1.add(isdhv);
        }

        System.out.println("总结果");
        for (ItripSearchDetailsHotelVO a:
                stringList1) {
            System.out.println(a);
        }

        return DtoUtil.returnSuccess("查询成功", stringList1);
    }

    @GetMapping("/api/hotel/queryhotelpolicy/{id}")
    public Dto queryhotelpolicy(@PathVariable("id") Integer id) {
        System.out.println(">>> 根据酒店id查询酒店政策ItripSearchPolicyHotelVO");
        System.out.println("酒店id:" + id);

        if (id == null) {
            return DtoUtil.returnError("10208 ", "酒店id不能为空", "");
        }

        ItripSearchPolicyHotelVO isphv = new ItripSearchPolicyHotelVO();

        QueryWrapper<ItripHotel> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);

        try {
            isphv.setHotelPolicy(iItripHotelService.getOne(wrapper).getHotelPolicy());
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("10209 ", "系统异常,获取失败", "");
        }

        return DtoUtil.returnSuccess("查询成功", isphv.getHotelPolicy());
    }


}
